import PubSub from 'pubsub-js'

/**
 * 事件总线，对Pubsub进行封装
 */
export class EventBus {
  /**
   * 消息订阅
   * @param topic 消息
   * @param listener 监听器
   */
  static subscribe<T>(topic: string, listener: (data: T) => void) {
    return PubSub.subscribe(topic, (_, data) => listener(data))
  }

  /**
   * 取消订阅
   * @param topic 消息
   */
  static unsubscribe(topic: string) {
    return PubSub.unsubscribe(topic)
  }

  /**
   * 发布消息
   * @param topic 消息
   * @param payload 窄体
   */
  static publish<T>(topic: string, payload: T) {
    PubSub.publish(topic, payload)
  }
}
